<template>
  <div class="login" v-loading="loading">
    <br>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="账号" prop="account" :error="verify.account">
        <el-input v-model.trim="ruleForm.account" ref="account" v-myfocus></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass" :error="verify.pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off" @keyup.13.native="login" ref="pass"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登陆</el-button>
        <el-button @click="register">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getData } from '../data/user'

export default {
  name: 'login',
  data () {
    var checkAccount = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('账号不能为空'))
      }
      return callback()
    }
    var checkPass = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入密码'))
      }
      return callback()
    }
    return {
      ruleForm: {
        pass: '',
        account: ''
      },
      rules: {
        account: [
          { validator: checkAccount, trigger: 'blur' }
        ],
        pass: [
          { validator: checkPass, trigger: 'blur' }
        ]
      },
      verify: {
        pass: '',
        account: ''
      },
      loading: true,
      client: []
    }
  },
  methods: {
    async login () {
      let data = this.client
      let {account: username, pass: password} = this.ruleForm
      this.$refs.ruleForm.validate((valid) => {
        console.log(valid)
        let arr = data.filter(o => o.username === username)
        if (valid && arr.length === 0) {
          this.verify.account = '账号名未注册'
        } else if (valid && arr.length !== 0) {
          if (arr[0].username !== username) {
            this.verify.account = '账号名错误'
            return
          }
          if (arr[0].password !== password) {
            this.verify.pass = '账号验证错误'
            return
          }
          localStorage.setItem('userInfo', JSON.stringify({username, password}))
          this.$router.push('/')
        } else {
          console.log(valid, arr)
          return false
        }
      })
    },
    register () {
      this.$router.push('/register')
    }
  },
  watch: {
    'ruleForm.pass' () {
      this.verify.pass = ''
    },
    'ruleForm.account' () {
      this.verify.account = ''
    }
  },
  async mounted () {
    this.client = await getData()
    this.loading = false
    this.$refs.account.focus()
    let userInfo = localStorage.getItem('userInfo')
    if (userInfo) {
      this.ruleForm.account = JSON.parse(userInfo).username
      this.$refs.pass.focus()
    }
    console.log(this.client, userInfo)
  }
}
</script>

<style lang="less" scoped>
.login {
  width: 60%;
  max-width: 600px;
  margin: 20% auto 0;
}
</style>
